import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { findById } from '../../services/order';

export const fetchOrder = createAsyncThunk('order/fetchOrder', async (id) => {
    let { data } = await findById(id);
    return data;
})

const orderSlice = createSlice({
    name: "order",
    initialState: {
        updateVisible: false,
        orderData: {
            user: {
                _id: ""
            },
            startTime: "",
            finishTime: "",
            status: 1
        }
    },
    reducers: {
        setUpdateVisible(state, action) {
            state.updateVisible = action.payload;
        }
    },
    extraReducers(builder) {
        builder.addCase(fetchOrder.fulfilled, (state, action) => {
            state.orderData = action.payload;
        })
    }
});

export const { setUpdateVisible } = orderSlice.actions;
export default orderSlice.reducer;